package E2013;
import org.omg.CORBA.portable.OutputStream;

import java.io.*;
import java.security.PrivateKey;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

/**
 *  序列化过程中，所有涉及到的实体类都必须序列化
 */

class myStr implements Serializable{

}

class students implements Serializable {  // 实现序列化接口
    // 定义序列化
    private static final long serialVersionUID = 1L;  // 长整型对应8个字节
    // 声明变量
    myStr strname;  //声明了mystr类型的变量，那么该类也必须序列化
    String name;
    int no;
    double score;
    // 封装有参构造方法
    public students(int no,String name, double score) {
        this.no = no;
        this.name = name;
        this.score = score;

    }
    // 重写toString方法
    @Override
    public String toString() {
        return  no+"\t"+name +"\t"+ score;
    }
}
public class e06 {
    public static void main(String[] args) {

        ObjectOutputStream os = null;
        ObjectInputStream oi = null;
        try{
             os = new ObjectOutputStream(new FileOutputStream("series.txt"));
            // 添加数据
            students stu1 = new students(19412,"jack",96.0);
            students stu2 =new students(19411,"lucy",98.0);
            students stu3 =new students(19416,"toam",94.5);
            students stu4 =new students(19420,"jimn",93.0);
            students stu5 =new students(19433,"flink",88.5);

            os.writeObject(stu1);
            os.writeObject(stu2);
            os.writeObject(stu3);
            os.writeObject(stu4);
            os.writeObject(stu5);

            oi = new ObjectInputStream(new FileInputStream("series.txt"));
            Object o1 = oi.readObject();
            Object o2 = oi.readObject();
            Object o3 = oi.readObject();
            Object o4 = oi.readObject();
            Object o5 = oi.readObject();
            System.out.println(o1);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (oi!=null){
                try {
                    oi.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}